using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class TestWebClient : MonoBehaviour
{

    public Text text;

    public RawImage image;

    public RenderTexture texture;

    public InputField input;

    public static string inputtext = "";

    private AssetBundle previousAssetBundle;

    // Start is called before the first frame update
    void Start()
    {
        input.onEndEdit.AddListener(onInputEndEdit);
    }

    public void onInputEndEdit(string value)
    {
        Debug.Log("用户输入了：" + value);
        inputtext = value;
        Debug.Log(" 私有变量 inputtext：" + inputtext);
    }

    public void onClickGetTextButton()
    {
        StartCoroutine(GetText());
    }

    public void onClickGetTexureButton()
    {
        StartCoroutine(GetTexture());
    }

    public void  onClickGetAssetBundle()
    {
        StartCoroutine(GetAssetBundle());
    }

    public void onClickPostTextBuntton()
    {
        Debug.Log("要上传的文本："+ inputtext);
        string txt = inputtext;
        StartCoroutine(PostText(txt));
    }

    public void onClickClearCanvas()
    {
        image.texture =  null;
        text.text = "Click Button, Web Request GET Text will show here ......";
    }


    IEnumerator GetText() {

        Debug.Log("download text");
        UnityWebRequest www = UnityWebRequest.Get("http://localhost:8080/text");
        yield return www.SendWebRequest();

        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
        }
        else 
        {
            Debug.Log(www.downloadHandler.text);
            text.text = www.downloadHandler.text;
        }
    }

    IEnumerator PostText(string inputtxt)
    {
        Debug.Log("modify text");
        Debug.Log("输入的 input text:" + inputtxt);
        string txt = inputtxt;
        if (txt == "")
        {
            txt = "hi there";
        }
        List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
        formData.Add(new MultipartFormDataSection($"text={txt}"));

        UnityWebRequest www = UnityWebRequest.Post("http://localhost:8080/text", formData);
        yield return www.SendWebRequest();
        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
        }
        else 
        {
            Debug.Log(www.downloadHandler.text);
            text.text = www.downloadHandler.text;
        }

    }

    IEnumerator GetTexture() {
        Debug.Log("download texture");
        //UnityWebRequest www = UnityWebRequestTexture.GetTexture("http://localhost:8080/image");
        string url = "https://gitee.com/harrysimply/personal-picture-bed/raw/master/image.JPG";
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
        yield return www.SendWebRequest();
        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
        }
        else 
        {
            Texture mytexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
            image.texture = mytexture;
        }
    }

    IEnumerator GetAssetBundle() {
        Debug.Log("download assetbundle");
        UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle("http://localhost:8080/assetbundle");
        yield return www.SendWebRequest();

        image.texture = texture;

        // 如果之前加载过 AB 那么需要先卸载，否则会报错
        if (previousAssetBundle != null) 
        {
            previousAssetBundle.Unload(true);
        }

        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
        }
        else 
        {
            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
            Object[] obj = bundle.LoadAllAssets<GameObject>();
            foreach (Object o in obj)
            {
                GameObject dlcObj = Instantiate(o) as GameObject;
                dlcObj.transform.position = new Vector3(-500,0,0);
            }
            previousAssetBundle = bundle;


        }
    }

}
